const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: false }));
const db = require('./database/index')
app.post('/api/reguser', (req, res) => {
    // console.log(req.body);
    let { username, password } = req.body;
    let sql = `select * from user where username="${username}"`;
    db(sql, (err, results) => {
        if (err) {
            return res.send({ status: 1, msg: 'sql语法错误' + err.username });
        }
        if (results.length >= 1) {
            return res.send({ status: 1, msg: '用户名已存在' });
        }
        const { md5 } = require('utility');
        password = md5(password);
        let sql = `insert into user set username="${username}",password="${password}"`;
        db(sql, (err, results) => {
            if (err) {
                return res.send({ status: 1, msg: 'sql语法错误: ' + err.message });
            }
            if (results.affectedRows !== 1) {
                return res.send({ status: 1, msg: '添加失败' });
            }
            res.send({ status: 0, msg: '注册成功！' });
        })
        res.send({ status: 0, msg: '注册成功' });
    })
    // res.send('注册成功 OK！');
});
app.listen(3006, () => {
    console.log('server running at: http://localhost:3006');
})